package com.kdtech.analyse.outside;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

import com.kdtech.analyse.AnalyseNews;
import com.kdtech.analyse.JSoupUtils;
import com.kdtech.crawler.CrawlHTML;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.RegexUtils;
import com.kdtech.utils.StringUtils;

/**
 * 阿波罗评论
 * @author KK
 *
 */
public class AboluowangNewsAnalyse implements AnalyseNews {

	private static final String[] regex={
		"http://www.aboluowang.com/[0-9]*/[0-9]*/[0-9]*.html",
		"http://bbs.aboluowang.com/forum.php\\?mod=viewthread&tid=[0-9]*&extra=.*",
		"http://bbs.aboluowang.com/forum.php\\?extra=page%3D1&mod=viewthread&tid=[0-9]*",
		"http://bbs.aboluowang.com/forum.php\\?.*mod=viewthread&tid=[0-9]*",

	};


	
	public boolean isDetailPage(String url) {
		if(url.contains("mobile")){
			return false;
		}
		return RegexUtils.matchAnyIgnoreCase(url, regex);
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta news=new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String htmltxt=urlMeta.getHtml();
		String url=urlMeta.getUrl();

		news.setUrl(url);
		String title=null;
		String content=null;
		String author=null;
		Long date=null;
		Document doc=Jsoup.parse(htmltxt);
		title =doc.select("#Article h1").text();
		content=HtmlCleaner.getContentHtml(url,doc.select("#summary,#content p"));
		if(StringUtils.isBlank(title)){
			title =doc.select("h1").text();
		}

		date=DateUtils.matchDate(doc.select(".articletime").text());
		author=JSoupUtils.matchAuthor(doc, "编辑：","来源:");


		Element first=doc.select("div[id^=post_]").first();
		if(first!=null){
			date=DateUtils.matchDate(first.select("div.pti").text());
			author=first.select("a.xw1").text();
			content=HtmlCleaner.getContentHtml(url,first.select("td.t_f"));
		}
		if(date==null)
			date = DateUtils.matchDate(url);
		
		String commTxt ="";
		Integer clickNum=null;
		Integer commnetNum=null;
		if (clickNum == null && commnetNum==null ){
			commTxt=doc.select("div.ptn").text();
			clickNum=NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "查看"));
			commnetNum=NumberUtils.matchNumber(StringUtils.substringAfter(commTxt, "回复"));
		}
		news.setTitle(StringUtils.trimSpace(title));
		news.setContent(content);
		news.setAuthor(author);
		news.setDate(date);
		news.setClickNum(clickNum);
		news.setCommentNum(commnetNum);

		return news;
	}

	

	

	
}
